
package com.kugou.game.sdk.ui.fragment;

import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.base.BaseSDKWorkerFragment;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.FAQListItem;
import com.kugou.game.sdk.interfaces.OnLoadFAQDetailListener;
import com.kugou.game.sdk.ui.activity.CommonSdkActivity;
import com.kugou.game.sdk.ui.widget.TipsLayout;
import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 描述:FAQ详情
 *
 * @author jamywang
 * @since 2015年11月17日 下午2:57:12
 */
public class FAQDetailFragment extends BaseSDKWorkerFragment {

    private static final int MSG_REQUEST_FAQ_DETAIL = 0;

    private static final int MSG_SHOW_LOADING_VIEW = 1;

    protected static final int MSG_REQUEST_FAQ_DETAIL_SUCCESS = 2;

    protected static final int MSG_REQUEST_FAQ_DETAIL_FAILE = 3;

    private Activity context;

    private int titleId;

    /**
     * 页面控件
     */
    private TextView mTitle;

    private TextView mContent;

    private LinearLayout detailLayout;

    private TipsLayout mTipsLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = null;
        view = inflater.inflate(Rx.layout.kg_fragment_faq_detail, null);
        if (context != null && context instanceof CommonSdkActivity) {
            ((BaseCommonTitleFragmentActivity) context).setFAQPageTitleStyle();
        }
        initview(view);
        titleId = context.getIntent().getIntExtra("id", 0);
        sendEmptyBackgroundMessage(MSG_REQUEST_FAQ_DETAIL);
        return view;
    }

    public void initview(View view) {
        detailLayout = (LinearLayout) view.findViewById(Rx.id.kg_faq_detail_layout);
        mTitle = (TextView) view.findViewById(Rx.id.kg_faq_title);
        mContent = (TextView) view.findViewById(Rx.id.kg_faq_content);
        mTipsLayout = (TipsLayout) view.findViewById(Rx.id.kg_tips_layout);
        mTipsLayout.setLoadingText("");
        mTipsLayout.setLoadingViewBackground(Rx.drawable.kg_gamecenter_loading);
        mTipsLayout.setOnRefreshButtonClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                sendEmptyBackgroundMessage(MSG_REQUEST_FAQ_DETAIL);
            }
        });
    }

    private void showLoadingView() {
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_LOADING);
        detailLayout.setVisibility(View.GONE);
    }

    private void showDetailLayout() {
        mTipsLayout.setVisibility(View.GONE);
        detailLayout.setVisibility(View.VISIBLE);
    }

    private void showLayoutResult() {
        detailLayout.setVisibility(View.GONE);
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_FAILE);
    }

    private void showLayoutEmpty() {
        detailLayout.setVisibility(View.GONE);
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_EMPTY_CONTENT);
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        final Message message = new Message();
        switch (msg.what) {
            case MSG_REQUEST_FAQ_DETAIL:
                sendEmptyUiMessage(MSG_SHOW_LOADING_VIEW);
                UserManager.getInstance().requestFAQDetail(titleId, new OnLoadFAQDetailListener() {

                    @Override
                    public void onSuccess(FAQListItem item) {
                        message.what = MSG_REQUEST_FAQ_DETAIL_SUCCESS;
                        message.obj = item;
                        sendUiMessage(message);
                    }

                    @Override
                    public void onFailed(String errorMsg) {
                        message.what = MSG_REQUEST_FAQ_DETAIL_FAILE;
                        message.obj = errorMsg;
                        sendUiMessage(message);
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    public void handleUiMessage(Message msg) {
        switch (msg.what) {
            case MSG_SHOW_LOADING_VIEW:
                showLoadingView();
                break;
            case MSG_REQUEST_FAQ_DETAIL_SUCCESS:
                FAQListItem itemDetail = (FAQListItem) msg.obj;
                if (itemDetail.getContent() != null) {
                    mContent.setText(Html.fromHtml(itemDetail.getContent()));
                    mTitle.setText(itemDetail.getTitle());
                } else {
                    showLayoutResult();
                }
                showDetailLayout();
                break;
            case MSG_REQUEST_FAQ_DETAIL_FAILE:
                showLayoutResult();
                String error = (String) msg.obj;
                showToast(error);
                break;
            default:
                break;
        }
    }

}
